package ctn.mail;

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import ctn.contant.PlugContant;
import ctn.util.StringUtils;

/**
 * send the email
 * 
 * @author HLJ
 * 
 */
public class EmailSend {

	private String tomail;
	private String frommail;
	private String subject;
	private String content;
	private String filePath;
	private boolean isAttachment = false;
	private String smtp;
	private String fromUser;
	private String fromPwd;
	private String sslFactory;
	private String port;
	private String quitwait;
	private String factoryPort;
	private String fallback;
	private String ssl;
	private boolean isHtmlBody;

	public boolean isHtmlBody() {
		return isHtmlBody;
	}
	public void setHtmlBody(boolean isHtmlBody) {
		this.isHtmlBody = isHtmlBody;
	}

	public String getSslFactory() {
		return sslFactory;
	}

	public void setSslFactory(String sslFactory) {
		this.sslFactory = sslFactory;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getQuitwait() {
		return quitwait;
	}

	public void setQuitwait(String quitwait) {
		this.quitwait = quitwait;
	}

	public String getFactoryPort() {
		return factoryPort;
	}

	public void setFactoryPort(String factoryPort) {
		this.factoryPort = factoryPort;
	}

	public String getFallback() {
		return fallback;
	}

	public void setFallback(String fallback) {
		this.fallback = fallback;
	}

	public String getSsl() {
		return ssl;
	}

	public void setSsl(String ssl) {
		this.ssl = ssl;
	}

	public String getSmtp() {
		return smtp;
	}

	public void setSmtp(String smtp) {
		this.smtp = smtp;
	}

	public String getFromUser() {
		return fromUser;
	}

	public void setFromUser(String fromUser) {
		this.fromUser = fromUser;
	}

	public String getFromPwd() {
		return fromPwd;
	}

	public void setFromPwd(String fromPwd) {
		this.fromPwd = fromPwd;
	}

	public boolean isAttachment() {
		return isAttachment;
	}

	public void setAttachment(boolean isAttachment) {
		this.isAttachment = isAttachment;
	}

	public String getTomail() {
		return tomail;
	}

	public void setTomail(String tomail) {
		this.tomail = tomail;
	}

	public String getFrommail() {
		return frommail;
	}

	public void setFrommail(String frommail) {
		this.frommail = frommail;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	private void setProperty(Properties p) {
		if (!StringUtils.isEmptyString(sslFactory)) {
			p.setProperty(PlugContant.E_MAIL_STAMP_SOCKET_FACTORY_CLASS,
					sslFactory);
		}
		if (!StringUtils.isEmptyString(fallback)) {
			p.setProperty(PlugContant.E_MAIL_STAMP_SOCKET_FACTORY_FALLBACK,
					fallback);
		}
		if (!StringUtils.isEmptyString(port)) {
			p.setProperty(PlugContant.E_MAIL_STAMP_PORT, port);
		}
		if (!StringUtils.isEmptyString(factoryPort)) {
			p.setProperty(PlugContant.E_MAIL_STAMP_SOCKET_FACTORY_PORT, factoryPort);
		}
		if (!StringUtils.isEmptyString(ssl)) {
			p.setProperty(PlugContant.E_MAIL_STAMP_SSL, ssl);

		}
		if (!StringUtils.isEmptyString(quitwait)) {
			p.setProperty(PlugContant.E_MAIL_STAMP_QUITWAIT, quitwait);
		}
	}

	/**
	 * this function to send mail
	 * 
	 * @return
	 */
	public boolean sendMail() {
		try {
			Properties p = System.getProperties();
			p.setProperty(PlugContant.E_MAIL_STAMP_HOST, this.smtp);
			p.setProperty(PlugContant.E_MAIL_STAMP_AUTH,
					PlugContant.E_MAIL_STAMP_AUTH_TRUE); // set the validate mode with true
			setProperty(p);
			Session session = Session.getDefaultInstance(p, new EmailCa(
					this.fromUser, this.fromPwd));
			MimeMessage msg = new MimeMessage(session);
			msg.setSentDate(new Date());
			InternetAddress fromAddress = new InternetAddress(this.frommail);
			msg.setFrom(fromAddress);
			InternetAddress[] toAddresses = { new InternetAddress(this.tomail) };
			msg.setRecipients(Message.RecipientType.TO, toAddresses);
			if (isHtmlBody) {
				msg.setContent(this.content,"text/html;charset = utf-8");
			}else{
				msg.setText(this.content);
			}
			msg.setSubject(this.subject);
			// if bring the attachment
			if (isAttachment) {
				Multipart mp = new MimeMultipart();
				MimeBodyPart mbp = new MimeBodyPart();
				FileDataSource fds = new FileDataSource(this.filePath);
				// get the attachment and set in the BodyPart
				mbp.setDataHandler(new DataHandler(fds));
				// get the file name and set in the BodyPart
				mbp.setFileName(fds.getName());
				mp.addBodyPart(mbp);
				msg.setContent(mp);
			}

			// send the mail
			Transport.send(msg);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

}
